package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.entity.AclAdmin;
import com.atguigu.entity.HouseBroker;
import com.atguigu.service.AdminService;
import com.atguigu.service.HouseBrokerService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/houseBroker")
public class HouseBrokerController extends BaseController {
    @Reference
    private AdminService adminService;

    @Reference
    private HouseBrokerService houseBrokerService;
    private static final String PAGE_CREATE = "houseBroker/create";

    @RequestMapping("/create")
    public String create(Model model, HouseBroker houseBroker) {
        //查询管理员列表
        List<AclAdmin> adminList = adminService.findAll();
        model.addAttribute("adminList", adminList);
        model.addAttribute("houseBroker", houseBroker);
        return PAGE_CREATE;
    }

    @PostMapping("/save")
    public String save(HouseBroker houseBroker, Model model) {
        AclAdmin admin = adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        if (StringUtils.isEmpty(admin.getHeadUrl())) {
            houseBroker.setBrokerHeadUrl(null);
        } else {
            houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        }


        houseBrokerService.insert(houseBroker);

        return successPage(model, "添加经纪人成功");
    }

    private static final String PAGE_EDIT = "houseBroker/edit";

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        HouseBroker houseBroker = houseBrokerService.getById(id);
        List<AclAdmin> adminList = adminService.findAll();
        model.addAttribute("adminList", adminList);
        model.addAttribute("houseBroker", houseBroker);

        return PAGE_EDIT;
    }

    /**
     * @param id       : 当前经纪人id
     * @param brokerId : 新经纪人的brokerId
     * @return
     */
    @PostMapping("/update/{id}")
    public String update(@PathVariable Long id, Long brokerId, Model model) {
        HouseBroker currentHouseBroker = houseBrokerService.getById(id);
        AclAdmin admin = adminService.getById(brokerId);
        currentHouseBroker.setBrokerId(admin.getId());
        currentHouseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        currentHouseBroker.setBrokerName(admin.getName());
        houseBrokerService.update(currentHouseBroker);
        return successPage(model, "修改经纪人成功!");
    }

    private static final String SHOW_ACTION = "redirect:/house/";

    @RequestMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable("houseId") Long houseId, @PathVariable("id") Long id) {
        houseBrokerService.delete(id);

        return SHOW_ACTION + houseId;
    }
}